﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Proggy.Data.Entities;
using Proggy.Data.Abstract;

namespace Proggy.Data.Concrete
{
    /// <summary>
    /// Data context
    /// </summary>
    public class ProggyDataContext : DbContext, IProggyDataContext
    {
        /// <summary>
        /// Publishable nodes
        /// </summary>
        public IDbSet<PublishableNode> PublishableNodes { get; set; }

        /// <summary>
        /// Draft nodes
        /// </summary>
        public IDbSet<DraftNode> DraftNodes { get; set; }

        /// <summary>
        /// Trashed, but not deleted nodes
        /// </summary>
        public IDbSet<TrashedNode> TrashedNodes { get; set; }

        /// <summary>
        /// The member groups
        /// </summary>
        public IDbSet<Group> Groups { get; set; }

        /// <summary>
        /// The group members
        /// </summary>
        public IDbSet<GroupMember> GroupMembers { get; set; }

        /// <summary>
        /// Members created via forms authentication
        /// </summary>
        public IDbSet<FormsAuthenticationMember> FormsAuthenticationMembers{get;set;}

        /// <summary>
        /// Random data
        /// </summary>
        public IDbSet<RandomDataItem> RandomDataItems { get; set; }

        /// <summary>
        /// Set model
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //
            base.OnModelCreating(modelBuilder);

            // set parent-child nodes
            modelBuilder.Entity<PublishableNode>()
                .HasKey(p=>p.PublishableNodeId)
                .HasOptional(n => n.ParentNode)
                .WithMany(n => n.ChildNodes)                
                .WillCascadeOnDelete(false);

            // set draft
            modelBuilder.Entity<DraftNode>()
                .HasKey(d => d.DraftNodeId);

            // set related in reverse?
            modelBuilder.Entity<PublishableNode>()
                .HasOptional(p => p.DraftNode);

            // trashed
            modelBuilder.Entity<TrashedNode>()
                .HasKey(t => t.TrashedNodeId);
            
            // groups
            modelBuilder.Entity<GroupMember>()
                .HasRequired(g => g.Group)
                .WithMany(g => g.GroupMembers)
                .WillCascadeOnDelete(true);    
           
            // group-node
            modelBuilder.Entity<PublishableNode>()
                .HasMany(n => n.AllowedEditorGroups)
                .WithMany(g => g.AllowedEditingNodes)
                .Map(m => {
                    m.ToTable("PublishableNode_AllowedEditorGroups");
                }) ;

            // group-node 2
            modelBuilder.Entity<PublishableNode>()
                .HasMany(n => n.AllowedViewerGroups)
                .WithMany(g => g.AllowedViewingNodes)
                .Map(m => {
                    m.ToTable("PublishableNode_AllowedViewerGroups");
                });
                

        }
    }
}
